<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/17
 * Time: 11:59
 */

namespace addons\admin\controller;


use app\common\AliPayAccount;
use app\common\model\Trade;
use app\common\model\Users;
use app\common\PaySuccess;
use app\common\Session;
use think\addons\Controller;

class Common extends Controller
{
    public static $base_config = [
        "ROOT_PATH" => ADDON_PATH,
        "DATA_PATH" => ADDON_PATH . "/admin/data/",
    ];

    /**
     * 是不是管理员
     * @return bool
     */
    function is_admin()
    {
        $admin = Session::get_local_admin();
        return !empty($admin);
    }

    function check_login()
    {
        if ($this->is_admin()) {

        } else {
            $this->error("请登录", "/addons/execute/admin-User-login");
        }
    }


    /**
     * 转账接口
     * @param AliPayAccount $account
     * @param $money
     * @throws \Exception
     */
    protected function zhuan_zhang( AliPayAccount $account, $money, $success_callback = null, $error_callback = null)
    {
        $out_biz_no = "alipay" . date("YmdHis") . rand(10000, 99999);
        $payee_type = 'ALIPAY_LOGONID';
        $payee_account = $account->payee_account;
        $amount = is_numeric($money) ? $money : 0;
        $payer_show_name = '新桃源';
        $payee_real_name = $account->real_name;
        $remark = sprintf("交易号%s 申请提现审批发放", $out_biz_no);
        common_vendor('alipay_Aop.aop.SignData');
        common_vendor('alipay_Aop.aop.AopClient');
        common_vendor('alipay_Aop.aop.request.AlipayFundTransToaccountTransferRequest');
        $aop = new \AopClient();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = config('alipay_appid');
        $aop->rsaPrivateKey = config('alipay_rsa_private');
        $aop->alipayrsaPublicKey = config('alipay_rsa_public');
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset = 'UTF-8';
        $aop->format = 'json';
        $request = new \AlipayFundTransToaccountTransferRequest();
        $request->setBizContent(
            sprintf("{" .
                "\"out_biz_no\":\"%s\"," .
                "\"payee_type\":\"%s\"," .
                "\"payee_account\":\"%s\"," .
                "\"amount\":\"%s\"," .
                "\"payer_show_name\":\"%s\"," .
                "\"payee_real_name\":\"%s\"," .
                "\"remark\":\"%s\"" .
                "}",
                $out_biz_no, $payee_type, $payee_account, $amount, $payer_show_name, $payee_real_name, $remark
            )
        );
        $result = $aop->execute($request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $msg = $result->$responseNode->msg;
        $sub_msg = isset($result->$responseNode->sub_msg) ? $result->$responseNode->sub_msg : "转账成功";
        $resultCode = $result->$responseNode->code;
        $this->assign("code", $resultCode);
        $this->assign("sub_msg", $sub_msg);
        $this->assign("msg", $msg);
        do {
            if (!empty($resultCode) && $resultCode == 10000) {
                call_user_func_array($success_callback, [
                    $out_biz_no, $money, $remark
                ]);
                break;
            }
            call_user_func_array($error_callback, [
                $resultCode, $sub_msg, $msg
            ]);

        } while (0);
    }


}